package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.common.PageInfo;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.vo.EmployeeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author YangJiaHao
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public EmployeeVo login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee==null){
            return null;
        }
        //生成token
        String token = "employee_token" + System.currentTimeMillis() + employee.getId();
        // String key = "djfhhsajfsdfbh";
        // Map<String,Object> map = new HashMap<>();
        // map.put("username", employee.getUsername());
        //
        // String token = JWTUtil.createToken(map, key.getBytes());

        //存入Redis数据库   JSON.toJSONString(employee) ->将对象转为json字符串
        redisTemplate.opsForValue().set("employee_token:"+token, JSON.toJSONString(employee),30, TimeUnit.MINUTES);
        employeeMapper.updateToken(token, employee.getId());
        //封装
        EmployeeVo employeeVo = new EmployeeVo();
        //将原始数据中与新数据共同的内容赋值给新数据
        BeanUtils.copyProperties(employee, employeeVo);
        employeeVo.setToken(token);

        return employeeVo;
    }

    @Override
    public void clearToken(String token) {
        Boolean res = redisTemplate.hasKey("employee_token:" + token);
        if (res==true){
            redisTemplate.delete("employee_token:" + token);
        }
    }

    @Override
    public EmployeeVo getById(String id) {

        Employee employee = employeeMapper.getById(id);
        if (employee==null){
            return null;
        }
        EmployeeVo employeeVo = new EmployeeVo();
        BeanUtils.copyProperties(employee, employeeVo);
        return employeeVo;
    }

    @Override
    public PageInfo<Employee> page(String name, Integer page, Integer pageSize){
        PageInfo<Employee> pageInfo = new PageInfo<>();
        Integer start = (page-1) * pageSize;
        List<Employee> employees = employeeMapper.page(name, start, pageSize);
        pageInfo.setRecords(employees);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        pageInfo.setTotal(employeeMapper.countEmployee(name));
        return pageInfo;
    }

    @Override
    public Boolean save(Employee employee) {
        Snowflake snowflake = IdUtil.getSnowflake();
        String id = snowflake.nextIdStr();
        employee.setId(id);
        Integer res = employeeMapper.save(employee);
        return res>0;
    }

    @Override
    public Employee findByUsername(String username){
        Employee employee = employeeMapper.findByUsername(username);
        return employee;
    }

    @Override
    public boolean updateEmployee(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        Integer integer = employeeMapper.updateEmployee(employee);
        return integer>0;
    }
}
